import Vue from "vue";
import {request} from "../../../util/request"
import VModal from 'vue-js-modal';
Vue.use(VModal,{dialog:true});
import "../../../common/common.less";
import "./list.less";
import pluginAddress from "../../../plugins/address/Address";
import pluginAuto from "../../../plugins/auto/Auto"
import dialog from "../../../util/dialog";
new Vue({
    el:"#louie",
    data:{
        list:[],   //列表
        index:0  //操作的插件下标
    },
    created:function(){
        this.getList()
            .then(()=>{
                this.openPlugin();
            });
    },
    components:{
        "plugin-address":pluginAddress,
        "plugin-auto":pluginAuto
    },
    methods:{
        // Run
        /**
         * 获取列表
         */
        getList(){
            return new Promise((success)=>{
                request("plugins/getPlugins")
                    .then((res)=>{
                        if(res.data.status === 'success'){
                            this.list = res.data.data;
                        }
                        success();
                    })
            });
        },
        /**
         * 是否显示默认打开的插件
         */
        openPlugin(){
            let hash = this.hash();
            if(hash.plugin){
                for(let i=0;i<this.list.length;i++){
                    if(this.list[i].value === hash.plugin){
                        this.index = i;
                        this.$modal.show('plugin-box');
                        break;
                    }
                }
            }
        },
        //解析链接hash
        hash:function(){
            var hash = window.location.hash;
            hash = hash.substring(1);
            if(hash == '') return {};
            var tempArr = hash.split('#');
            var data = {};
            for(var i=0;i<tempArr.length;i++){
                var tArr = tempArr[i].split('=');
                data[tArr[0]] = tArr[1];
            }
            return data;
        },

        // Handle
        /**
         * 打开某个插件设置界面
         * @param index
         */
        setPlugin(index){
            this.index = index;
            this.$modal.show("plugin-box");
        },
        /**
         * 更新插件清除缓存
         */
        updatePlugin(){
            request("plugins/updatePlugin")
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.getList();
                        dialog.alert(this,"更新成功");
                    }
                });
        }
    }
});